package com.cardbaobao.cardbabyclient.ui.adapter.base;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.utils.DensityUtil;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.PhotoImageLoader;

public class ViewHolder {

	private SparseArray<View> mViews;
	private View mConvertView;
	private Context mContext;
	/**
	 * item 位置
	 */
	private int mPosition;
	/**
	 * item布局id
	 */
	private int mLayoutId;

	public ViewHolder(Context context,ViewGroup parent,int layoutId,int position){
		this.mViews = new SparseArray<>();
		this.mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
		this.mContext = context;
		this.mPosition = position;
		mConvertView.setTag(this);
	}

	/**
	 * 获取ViewHolder对象
	 * @param context
	 * @param convertView
	 * @param parent
	 * @param layoutId item layout id
	 * @param position getView 中的position位置
	 * @return
	 */
	public static ViewHolder get(Context context,View convertView,ViewGroup parent,int layoutId,int position){
		if(convertView == null){
			ViewHolder viewHolder = new ViewHolder(context, parent, layoutId, position);
			viewHolder.mLayoutId = layoutId;
			return viewHolder;
		}
		return (ViewHolder) convertView.getTag();
	}
	/**
	 * 根据控件的id获取对应的控件，如果没有则加入Views
	 * @param viewId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T extends View> T getView(int viewId){
		View view = mViews.get(viewId);
		if(view == null){
			view = mConvertView.findViewById(viewId);
			mViews.put(viewId, view);
		}
		return (T) view;
	}


	public View getConvertView(){
		return mConvertView;
	}

	/**
	 * TextView 设置text
	 * @param viewId TextView ID
	 * @param text 文本内容
	 * @return
	 */
	public ViewHolder setText(int viewId,String text){
		TextView view = getView(viewId);
		view.setText(text);
		return this;
	}

	/**
	 * ImageView 设置图片源
	 * @param viewId ImageView ID
	 * @param drawableId 源文件ID
	 * @return
	 */
	public ViewHolder setImageResource(int viewId,int drawableId){
		ImageView view = getView(viewId);
		view.setImageResource(drawableId);
		return this;
	}

	/**
	 * ImageView 设置Bitmap对象
	 * @param viewId ImageView ID
	 * @param bm Bitmap对象
	 * @return
	 */
	public ViewHolder setImageBitmap(int viewId,Bitmap bm){
		ImageView view = getView(viewId);
		view.setImageBitmap(bm);
		return this;
	}

	/**
	 * 圆角
	 * @param viewId
	 * @param imgUrl
	 * @param defaultImgRes
     * @return
     */
	public ViewHolder displayCornerImage(int viewId,String imgUrl,int defaultImgRes){
		ImageLoaderUtil.displayImageWithPlaceHolder(imgUrl,
				(ImageView)getView(viewId),
				defaultImgRes,
				DensityUtil.dp2px(8));
		return this;
	}

	/**
	 * 不是圆角
	 * @param viewId
	 * @param imgUrl
	 * @param defaultImgRes
     * @return
     */
	public ViewHolder displayImage(int viewId,String imgUrl,int defaultImgRes){
		ImageLoaderUtil.displayImageWithPlaceHolder(imgUrl,(ImageView)getView(viewId),defaultImgRes);
		return this;
	}

	/**
	 * 返回 item 位置
	 * @return
	 */
	public int getPosition(){
		return mPosition;
	}

	/**
	 * 获取item布局Id
	 * @return
	 */
	public int getLayoutId()
	{
		return mLayoutId;
	}

	/**
	 * 为ImageView设置本地图片
	 *
	 * @param viewId
	 * @param url
	 * @return
	 */
	public ViewHolder setImageByUrl(int viewId, String url) {
		PhotoImageLoader.getInstance(3, PhotoImageLoader.Type.LIFO).loadImage(url,
				(ImageView) getView(viewId));
		return this;
	}

	/**
	 * 为TextView设置是否隐藏
	 *
	 * @param viewId
	 * @param position
	 * @return
	 */
	public ViewHolder setSelectedImgVisible(int viewId, int position) {
		ImageView view = getView(viewId);
		if (position == mPosition) {
			view.setVisibility(View.VISIBLE);
		} else {
			view.setVisibility(View.INVISIBLE);
		}
		return this;
	}
}
